<template>
  <div class="search">
    <div class="inp">
      <van-icon name="arrow-left" @click="backFn" />
      <van-search
        @keydown.enter="Food_suggestFn(value)"
        v-model="value"
        placeholder="请输入搜索关键词"
      />
      <van-icon name="delete-o" @click="removearrFn()" />
    </div>
    <div class="zanwei"></div>
    <!-- 自行搜索 -->
    <div class="Food_list" v-if="this.$store.state.FoodsuggestList.length != 0">
      <ul>
        <li v-for="item in this.$store.state.FoodsuggestList" :key="item.id">
          <div>
            <img :src="item.img" alt="" />
          </div>
          <div class="message_item">
            <p>{{ item.name }}</p>
            <p class="p_text2">{{ item.tips }}</p>
            <p><van-icon name="star-o" />{{ item.ph }}</p>
            <p><van-icon name="fire-o" />{{ item.pw }}</p>
          </div>
        </li>
      </ul>
    </div>
    <div v-else style="text-align: center; color: #ccc">暂无搜索</div>

    <!-- 热门搜索 Food_list存在就消失-->
    <div class="search_list" v-if="this.$store.state.FoodsuggestList == 0">
      <ul>
        <li
         v-for="item in this.$store.state.searchList" 
         :key="item.id"
         @click="updateSelectedTextFn(item.title )">
          {{ item.title }}
        </li>
      </ul>
    </div>
   
  </div>
</template>


<script>
import { Food_hot, Food_search } from "../api/index";
export default {
  data() {
    return {
      value: "",
    };
  },
  created() {
    this.Food_hotFn();
  },
  methods: {
    removearrFn(){
    this.$store.state.FoodsuggestList=''
    },
    updateSelectedTextFn(text){
      this.value=text
      this.Food_suggestFn(this.value)
    },
    backFn() {
      this.$router.push({ path: "./" });
       this.$store.state.FoodsuggestList=''
    },
    async Food_hotFn() {
      await Food_hot().then((content) => {
        // console.log(content);
        let { state, result } = content;
        if (state == "success") {
          // console.log(result);
          let { suggestdetails } = result;
          // console.log(suggestdetails);
          let data = suggestdetails.map((item, index) => ({
            title: item.title,
            id: index,
          }));
          // console.log(data);
          this.$store.state.searchList = data;
          // console.log(this.$store.state.searchList);
        }
      });
    },
    async Food_suggestFn(value) {
      await Food_search(value).then((content) => {
        let { state, result } = content;
        if (state == "success") {
          // console.log(result);
          let { list } = result;
          // console.log(list);
          let data = list.map((item) => ({
            r: item.r,
          }));
          console.log("data", data);
          // 存在r,才渲染
          let filteredList = data.filter(
            (item) => item.hasOwnProperty("r") && item.r !== undefined
          );
          // console.log(filteredList); //过滤掉r为undefined
          let arr = [];
          filteredList.forEach((item) => {
            // console.log('item',item);
            let obj = {};
            obj.id = item.r.id;
            obj.img = item.r.img;
            obj.major = item.r.major;
            obj.name = item.r.n;
            obj.cook_time = item.r.cook_time;
            obj.ph = item.r.ph;
            obj.pw = item.r.pw;
            obj.tips = item.r.tips;
            obj.cookstory = item.r.cookstory;
            // console.log('obj',obj);
            arr.push(obj);
            // console.log('arr',arr);
          });
          this.$store.state.FoodsuggestList = arr;
          console.log(this.$store.state.FoodsuggestList);
          this.value = "";
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.zanwei {
  height: 80px;
}
.Food_list {
  padding: 0 15px;
  position: absolute;
  top: 40px;
  left: 0;
  width: 100%;
  ul {
    li {
      margin-top: 20px;
      display: flex;
      justify-content: space-around;
      align-items: center;
      p {
        margin-left: 10px;
        .van-icon {
         margin-right: 10px;
        }
      }
      img {
        width: 200px;
      }

      .p_text2 {
        font-size: 14px;
        width: 150px;
        display: -webkit-box; /* 必须的，作为弹性盒子模型显示 */
        -webkit-line-clamp: 2; /* 限制显示的文本行数 */
        -webkit-box-orient: vertical; /* 设置盒子的子元素的排列方式 */
        overflow: hidden; /* 隐藏溢出的内容 */
        text-overflow: ellipsis; /* 使用省略号表示文本被截断 */
      }
    }
  }
  .message_item {
    font-size: 14px;
  }
}
.inp {
  background-color: #fff;
  z-index: 999;
  position: fixed;
  top: 0;
  left: 0;
  padding: 0 15px;
  display: flex;
  width: 100%;
  justify-content: space-between;
  align-items: center;
  .van-search {
    width: 100%;
  }
}
.search_history {
  padding: 0 15px;
  display: flex;
  justify-content: space-between;
}
.search_list {
  margin-top: 20px;
  padding: 0 15px;
  ul {
    display: flex;
    flex-wrap: wrap;

    li {
      // width: 60px;
      height: 20px;
      text-align: center;
      margin-top: 20px;
      margin-left: 20px;
      background-color: #eeeeee;
      border-radius: 10px;
      font-size: 14px;
    }
  }
}
</style>
